package com.bnz.servlet;

import cn.hutool.core.util.StrUtil;
import com.alibaba.fastjson.JSON;
import com.bnz.pojo.Classes;
import com.bnz.pojo.PageResult;
import com.bnz.pojo.Student;
import com.bnz.pojo.StudentVo;
import com.bnz.service.ClassesService;
import com.bnz.service.StudentService;
import com.bnz.service.impl.ClassesServiceImpl;
import com.bnz.service.impl.StudentServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

@WebServlet("/student")
public class StudentServlet extends HttpServlet {
    private StudentService studentService;
    private ClassesService classesService;
    @Override
    public void init() {
        studentService = new StudentServiceImpl();
        classesService = new ClassesServiceImpl();
    }

    /**
     * 1. 根据不同的请求参数调用不同的处理方法
     * @param req
     * @param resp
     * @throws ServletException
     * @throws IOException
     */
    @Override
    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //1.0 处理post请求的中文乱码
        req.setCharacterEncoding("UTF-8");
        //1.1 设置响应对象的编码类型
        resp.setContentType("text/html;charset=utf8");
        //1.2 s到请求参数
        String cmd = req.getParameter("cmd");
        //1.3 根据参数的值来调用不同的方法
        if(StrUtil.isNotBlank(cmd)){
            if("list".equals(cmd)){         // 列表学生
                list(req,resp);
            }
            if("findByPage".equals(cmd)){  // 分页查询
                findByPage(req,resp);
            }
            if("search".equals(cmd)){       // 条件查询带分页
                search(req,resp);
            }

        }
    }

    /**
     * 分页查询
     * @param req
     * @param resp
     */
    private void findByPage(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //设置默认值：
        int page =1;
        int pageSize =5;
        //传参；
        String page1= req.getParameter("page");
        String pageSize1 = req.getParameter("pageSize");
        //判空
        page = StrUtil.isNotBlank(page1) ? new Integer(page1) : page;
        pageSize = StrUtil.isNotBlank(pageSize1) ? new Integer(pageSize1) : pageSize;
            //分页
        PageResult<Student> byPage = studentService.findByPage(page, pageSize);
        //获得班级列表
        List<Classes> all = classesService.findAll();
        //放入请求作用域中，
        req.setAttribute("byPage",byPage);
        req.setAttribute("classes",all);
        //通过请求转发到jsp
        req.getRequestDispatcher("WEB-INF/jsp/student/list.jsp").forward(req,resp);
    }


    /**
     * 2. 学生列表
     * @param req
     * @param resp
     * @throws ServletException
     * @throws IOException
     */
    private void list(HttpServletRequest req, HttpServletResponse resp)  throws ServletException, IOException {
        //2.1 查询所有学后
        List<Student> students = studentService.findAll();
        req.setAttribute("students",students);
        //2.3 进行页面跳转
        req.getRequestDispatcher("/WEB-INF/jsp/student/list.jsp").forward(req,resp);
    }

    // 条件查询带分页
    private void search(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
       //得到分页数据
        int page = 1;
        int pageSize =5;
        //得到前端传来的参数
        String page1 = req.getParameter("page");
        String pageSize1 = req.getParameter("pageSize");
        //判空
        page = StrUtil.isNotBlank(page1) ? new Integer(page1) : page;
        pageSize = StrUtil.isNotBlank(pageSize1) ? new Integer(pageSize1) : pageSize;
        //得到传的查询条件；
        String sname = req.getParameter("sname");
        String addr = req.getParameter("addr");
        String cid = req.getParameter("cid");
        if(StrUtil.isBlank(sname)){
            sname = "";
        }
        if(StrUtil.isBlank(addr)){
            addr = "";
        }
        if(StrUtil.isBlank(cid)){
            cid = "0";
        }
        //封装为对象
        StudentVo studentVo = new StudentVo(sname, addr,new Integer(cid));
        //分页查询
        PageResult<Student> pr = studentService.search(page, pageSize, studentVo);
        //查询班级
        List<Classes> all = classesService.findAll();
        //放到请求作用域
        req.setAttribute("pr",pr);
        req.setAttribute("classes",all);
        //转发
        req.getRequestDispatcher("/WEB-INF/jsp/student/list.jsp").forward(req,resp);


    }
}
